<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="handleClose"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="'110px'"
                    :rules="rules"
                    class="form"
                    :disabled="baTable.form.operate=='view'"
                >
                
                <div style="width:100%">
                    <FormItem :label="t('bill.TZID')" type="string" style="width:25%" v-model="baTable.form.items!.TZID" prop="TZID" :input-attr="{ placeholder: t('Please input field', { field: t('bill.TZID') }),disabled:true }" />
                </div>
                <FormItem 
                :label="t('bill.CORPCODE')" 
                v-model="baTable.form.items!.CORPCODE" 
                prop="corp" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'corp',
                    field: 'corp',
                    'remote-url': '/admin/tz/all?sql='+`select corp from corplib where active='1' `
                }"
 
                />
                <FormItem :label="t('bill.BILLID')" type="string" v-model="baTable.form.items!.BILLID" prop="BILLID" :input-attr="{ placeholder: t('Please input field', { field: t('bill.BILLID') }),disabled:true  }" />
                <FormItem :label="t('bill.MARKNO')" type="string" v-model="baTable.form.items!.MARKNO" prop="MARKNO" :input-attr="{ placeholder: t('Please input field', { field: t('bill.MARKNO') }),disabled:true  }" />
                <FormItem :label="t('bill.JOBNO')" type="string" v-model="baTable.form.items!.JOBNO" prop="JOBNO" :input-attr="{ placeholder: t('Please input field', { field: t('bill.JOBNO') }),disabled:true }" />
                <FormItem :label="t('bill.RATIO')" type="string" v-model="baTable.form.items!.RATIO" prop="RATIO" :input-attr="{ placeholder: t('Please input field', { field: t('bill.RATIO') }) }" />
                <FormItem 
                :label="t('bill.ITEMCODE')" 
                v-model="baTable.form.items!.ITEMCODE" 
                prop="ITEMCODE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'codename',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=itemcode'
                }"
                />
                <FormItem 
                :label="t('bill.BILLCLASS')" 
                v-model="baTable.form.items!.BILLCLASS" 
                prop="BILLCLASS" 
                type="select" 
                :data="{ content: { 0:'正常账单',1: '代垫',2:'AMENDMENT' } }"
                :input-attr="{ placeholder: t('Please input field', { field: t('bill.BILLCLASS') }) }" 
                />
                <FormItem 
                :label="t('bill.MONEYTYPE')" 
                v-model="baTable.form.items!.MONEYTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'moneytype',
                    field: 'moneytype',
                    'remote-url': '/admin/tz/all?sql='+`select moneytype from currentrate`
                }" 
                />
                <FormItem 
                :label="t('bill.CNTRNO')" 
                v-model="baTable.form.items!.CNTRNO" 
                prop="CNTRNO" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'cntrno',
                    field: 'cntrno',
                    'remote-url': '/admin/tz/all?sql='+`select cntrno from conloadplan where tzid='${baTable.form.items!.TZID||''}'`
                }" 
                />
                <FormItem :label="t('bill.AMOUNT')" type="string" v-model="baTable.form.items!.AMOUNT" prop="AMOUNT" :input-attr="{ placeholder: t('Please input field', { field: t('bill.AMOUNT') }) }" />
                <FormItem :label="t('bill.PRICETYPE')" type="string" v-model="baTable.form.items!.PRICETYPE" prop="PRICETYPE" :input-attr="{ placeholder: t('Please input field', { field: t('bill.PRICETYPE') }) }" />
                <FormItem :label="t('bill.MONEY')" type="number" prop="MONEY" v-model.number="baTable.form.items!.MONEY" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('bill.MONEY') }) }" />
                <FormItem 
                :label="t('bill.TOINVOICE')" 
                type="select" 
                v-model="baTable.form.items!.TOINVOICE" 
                prop="TOINVOICE" 
                :data="{ content: { 1:'专票',2: '普票',3:'debit' } }"
                :input-attr="{ placeholder: t('Please input field', { field: t('bill.TOINVOICE') }) }" 
                />
                <FormItem :label="t('bill.STARTDATE')" type="datetime" v-model="baTable.form.items!.STARTDATE" prop="STARTDATE" :input-attr="{ placeholder: t('Please select field', { field: t('bill.STARTDATE') }) }" />
                <FormItem :label="t('bill.ENDDATE')" type="datetime" v-model="baTable.form.items!.ENDDATE" prop="ENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('bill.ENDDATE') }) }" />
                <FormItem 
                :label="t('bill.TAXRATE')" 
                prop="TAXRATE" 
                v-model.number="baTable.form.items!.TAXRATE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=taxrate'
                }" 
                />
                <FormItem :label="t('bill.REMARK')" type="textarea" style="width:100%" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('bill.REMARK') }) }" />
                <FormItem :label="t('bill.OPNAME')" type="string" v-model="baTable.form.items!.OPNAME" prop="OPNAME" :input-attr="{ placeholder: t('Please input field', { field: t('bill.OPNAME') }),disabled:true }" />
                <FormItem :label="t('bill.OPDATETIME')" type="datetime" v-model="baTable.form.items!.OPDATETIME" prop="OPDATETIME" :input-attr="{ placeholder: t('Please select field', { field: t('bill.OPDATETIME') }),disabled:true }" />
                <FormItem :label="t('bill.OPDEPT')" type="string" v-model="baTable.form.items!.OPDEPT" prop="OPDEPT" :input-attr="{ placeholder: t('Please input field', { field: t('bill.OPDEPT') }),disabled:true }" />
                <FormItem :label="t('bill.BILLDATE')" type="datetime" v-model="baTable.form.items!.BILLDATE" :input-attr="{ placeholder: t('Please select field', { field: t('bill.BILLDATE') })}" />
                <FormItem 
                :label="t('bill.CHECKED')" 
                v-model="baTable.form.items!.CHECKED" 
                prop="CHECKED" 
                type="select" 
                :data="{ content: { 0:'未审',2: '已审核'} }"
                :input-attr="{ placeholder: t('Please input field', { field: t('bill.CHECKED') }),disabled:true }" />
                <FormItem :label="t('bill.CHECKER')" type="string" v-model="baTable.form.items!.CHECKER" prop="CHECKER" :input-attr="{ placeholder: t('Please input field', { field: t('bill.CHECKER') }),disabled:true }" />
                <FormItem :label="t('bill.CHECKDATETIME')" type="datetime" v-model="baTable.form.items!.CHECKDATETIME" prop="CHECKDATETIME" :input-attr="{ placeholder: t('Please select field', { field: t('bill.CHECKDATETIME') }),disabled:true }" />
                <FormItem 
                :label="t('bill.AUTOADD')" 
                type="radio" 
                v-model="baTable.form.items!.AUTOADD" 
                prop="AUTOADD" 
                :data="{ content: { 0:'手工',1: '自动' } }"
                />
                <FormItem :label="t('bill.ACTUALMONEY')" type="number" prop="ACTUALMONEY" v-model.number="baTable.form.items!.ACTUALMONEY" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('bill.ACTUALMONEY') }) }" />
                <FormItem :label="t('bill.VOUCHERNO')" type="string" v-model="baTable.form.items!.VOUCHERNO" prop="VOUCHERNO" :input-attr="{ placeholder: t('Please input field', { field: t('bill.VOUCHERNO') }),disabled:true }" />
                <FormItem :label="t('bill.INVOICEID')" type="string" v-model="baTable.form.items!.INVOICEID" prop="INVOICEID" :input-attr="{ placeholder: t('Please input field', { field: t('bill.INVOICEID') }),disabled:true }" />

                <!-- <FormItem :label="t('bill.INOUT')" type="string" v-model="baTable.form.items!.INOUT" prop="INOUT" :input-attr="{ placeholder: t('Please input field', { field: t('bill.INOUT') }) }" />
                <FormItem :label="t('bill.BILLTYPE')" type="radio" v-model="baTable.form.items!.BILLTYPE" prop="BILLTYPE" :data="{ content: { 1: t('bill.BILLTYPE 1') } }" :input-attr="{ placeholder: t('Please select field', { field: t('bill.BILLTYPE') }) }" />
                <FormItem :label="t('bill.RATE')" type="number" prop="RATE" v-model.number="baTable.form.items!.RATE" :input-attr="{ step: '0.0000000001', placeholder: t('Please input field', { field: t('bill.RATE') }) }" />
                <FormItem :label="t('bill.MARKNO')" type="string" v-model="baTable.form.items!.MARKNO" prop="MARKNO" :input-attr="{ placeholder: t('Please input field', { field: t('bill.MARKNO') }) }" />
                <FormItem :label="t('bill.yfmoney')" type="number" prop="yfmoney" v-model.number="baTable.form.items!.yfmoney" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('bill.yfmoney') }) }" />
                <FormItem :label="t('bill.flag1')" type="string" v-model="baTable.form.items!.flag1" prop="flag1" :input-attr="{ placeholder: t('Please input field', { field: t('bill.flag1') }) }" />
                <FormItem :label="t('bill.TJFLAG')" type="string" v-model="baTable.form.items!.TJFLAG" prop="TJFLAG" :input-attr="{ placeholder: t('Please input field', { field: t('bill.TJFLAG') }) }" />
                <FormItem :label="t('bill.TJOP')" type="string" v-model="baTable.form.items!.TJOP" prop="TJOP" :input-attr="{ placeholder: t('Please input field', { field: t('bill.TJOP') }) }" />
                <FormItem :label="t('bill.TJDATETIME')" type="datetime" v-model="baTable.form.items!.TJDATETIME" prop="TJDATETIME" :input-attr="{ placeholder: t('Please select field', { field: t('bill.TJDATETIME') }) }" /> -->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, watchEffect, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'

const TZID = inject('TZID')
const corp = inject('corp')
const emit = defineEmits(['on-after-save'])

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    TZID: [buildValidatorData({name: 'required', title: t('bill.TZID')})],
    INOUT: [buildValidatorData({name: 'required', title: t('bill.INOUT')})],
    BILLTYPE: [buildValidatorData({name: 'required', message: t('Please select field', { field: t('bill.BILLTYPE') })})],
    CORPCODE: [buildValidatorData({name: 'required', title: t('bill.CORPCODE')})],
    ITEMCODE: [buildValidatorData({name: 'required', title: t('bill.ITEMCODE')})],
    MONEY: [buildValidatorData({name: 'required', title: t('bill.MONEY')})],
    AUTOADD: [buildValidatorData({name: 'required', title: t('bill.AUTOADD')})],
    TOINVOICE: [buildValidatorData({name: 'required', title: t('bill.TOINVOICE')})],
    BILLCLASS: [buildValidatorData({name: 'required', title: t('bill.BILLCLASS')})],
    // BILLDATE: [buildValidatorData({name: 'required', message: t('Please select field', { field: t('bill.BILLDATE') })}), buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.BILLDATE') })})],
    ACTUALMONEY: [buildValidatorData({name: 'date', title: t('bill.ACTUALMONEY')})],
    CHECKED: [buildValidatorData({name: 'required', title: t('bill.CHECKED')})],
    OPDATETIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.OPDATETIME') })})],
    CHECKDATETIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.CHECKDATETIME') })})],
    STARTDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.STARTDATE') })})],
    ENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.ENDDATE') })})],
    TJDATETIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('bill.TJDATETIME') })})],
})


watchEffect(()=>{
    if( baTable.form.operate == 'add'){        
        baTable.form.items!.TZID = TZID? TZID.value : ''
        baTable.form.items!.CHECKED = 0
        baTable.form.items!.BILLCLASS = 0
        baTable.form.items!.TOINVOICE = 1
        baTable.form.items!.AUTOADD = 0
        baTable.form.items!.TAXRATE = 0
        baTable.form.items!.INOUT = 1
        baTable.form.items!.MONEYTYPE = 0
        if(baTable.form.items!.BILLTYPE == 0){
            baTable.form.items!.CORPCODE = corp.value
        }
        // baTable.form.items!.BILLTYPE = 0
        
    }
})


const handleClose = ()=>{
    baTable.toggleForm()
    emit('on-after-save')
}

</script>

<style scoped lang="scss">
.form{
    display: flex;
    flex-wrap: wrap;
    >div{
        width: 25%;
    }
}
</style>
